iT邦幫忙

2022 iThome 鐵人賽

DAY 6
0
Software Development

這些年,我們早該學會的Spring Framework系列 第 6

Day06 - Dependency Injection (1)

  • 分享至 

  • xImage
  •  

Review

Container的職責在於創建、配置與組裝bean,昨天我們學到了

  • Container的初始化是藉由ClassPathXmlApplicationContext
  • 物件實例在Container初始化完成就建立完畢
  • Container透過物件setter方法配置物件
  • 透過設定檔中的id可以取得物件
  • 同一個id的bean在Container只會有一個實例
  • 找不到bean的id會拋出NoSuchBeanDefinitionException

今日將探討其他取得bean的方式與Container其他配置物件的方式

獲取bean的幾種方式

  • 透過類別取得物件
@Test
public void testDay06(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
    User user= ioc.getBean(User.class);
    System.out.println(user);
}
  • bean設定檔中有同樣類別的bean,透過類別取得物件會拋出NoUniqueBeanDefinitionException
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user01" class="com.swj.User">
        <property name="name" value="James"></property>
        <property name="gender" value="male"></property>
        <property name="email" value="abc@test.com.tw"></property>
     </bean>
    <bean id="user02" class="com.swj.User">
        <property name="name" value="haha"></property>
        <property name="gender" value="male"></property>
        <property name="email" value="haha@test.com.tw"></property>
    </bean>
</beans>
@Test
public void testDay06(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
    User user= ioc.getBean(User.class);
    System.out.println(user);
}

Result
get_bean_01

  • 透過id與類別取得物件
@Test
public void testDay06(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
    User user= ioc.getBean("user01", User.class);
    System.out.println(user);
}

各種配置bean的方式

透過Constructor

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 略 user01、user02   -->
    <bean id="user03" class="com.swj.User">
        <constructor-arg name="email" value="123@test.com.tw"></constructor-arg>
        <constructor-arg name="name" value="kelly"></constructor-arg>
        <constructor-arg name="gender" value="female"></constructor-arg>
    </bean>
</beans>
public class User {
    //property、getter settet略
    public User(String name, String gender, String email) {
        this.name = name;
        this.gender = gender;
        this.email = email;
        System.out.println("呼叫有參建構子");
    }
}
@Test
public void testDay06(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
    User user= ioc.getBean("user03", User.class);
    System.out.println(user);
}

Result
get_bean_02

透過p-namespace配置bean

  • 設定檔中需要引入xmlns:p="http://www.springframework.org/schema/p"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 略 user01-user03  -->
    <bean id="user04" class="com.swj.User" p:email="222@test" p:gender="female" p:name="karan">
    </bean>
</beans>
@Test
public void testDay06(){
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
    User user= ioc.getBean("user04", User.class);
    System.out.println(user);
}

Result
get_bean_03


上一篇
Day05 - IoC你要知道的事
下一篇
Day07 - Dependency Injection (2)
系列文
這些年,我們早該學會的Spring Framework30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言